ARD2  RC2
Airbag Reference Demonstrator using MPC5604P
Labview.c
Go to the documentation of this file.
00001 
00018 #include "LIN_UART.h"
00019 #include "Labview.h"
00020 #include "utils.h"
00021 /*
00022  ******************************************************************************
00023  * constants
00024  ******************************************************************************
00025  */
00027 static const uint8_t cu8StartOfFrame[] = "[MS00[MP00[TI00[TS00[TT00";
00029 static const uint8_t cu8StartOfFrameD[] = "[DA0[DS0";
00031 static const uint8_t cu8StartOfFrameT[] = "[RS[RR[RE";
00033 static const uint8_t cu8StartOfFrameE[] = "[DD";
00035 static const uint8_t cu8EndofFrame = ']';
00037 const uint8_t cau8FakeTime[] = 
00038 {
00039   '1', '2', '3', '4'
00040 };
00041 void (* const pvfnMixNMatch[])(uint8_t* pu8Data, 
00042                                       uint8_t u8Length, 
00043                                       uint8_t u8Type) = 
00044     {
00045       &vfnLVMixAndMatch, &vfnLVMixAndMatchParallel
00046     };
00047 
00048 /*
00049  ******************************************************************************
00050  * Globals
00051  ******************************************************************************
00052  */
00054 uint8_t gau8LabViewMessage[LABVIEW_COMPOSITE_MESSAGE_SIZE];
00055 uint8_t gau8LabViewRxCmd[LABVIEW_CMD_MSG_SIZE];
00056 /*
00057  ******************************************************************************
00058  * vfnLVMixAndMatch
00059  ******************************************************************************
00060  */
00061 void vfnLVMixAndMatch(uint8_t* pu8Data, uint8_t u8Length, uint8_t u8Type)
00062 {
00063   uint8_t u8Status;
00064   uint8_t u8LengthInHexASCII[2u];
00065   
00066   /* Start transmission of start of frame while other things are figured out */
00067   u8Status = u8fnUARTWrite(LABVIEW_SCI, \
00068                              &(cu8StartOfFrame[SIZE_OF_SOF * u8Type]), \
00069                              SIZE_OF_SOF);
00070   if(CLEAR == u8Status)
00071   {
00072     /* Transform the length value into an ASCII reading - still in hex */
00073     vfnHexToASCII(&u8Length, (uint8_t*)u8LengthInHexASCII, sizeof(u8Length));
00074     
00075     /* Once it's done, send data. */
00076     (void)u8fnUARTWrite(LABVIEW_SCI, (const uint8_t*)&u8LengthInHexASCII, \
00077                         N_ELEMENTS(u8LengthInHexASCII));
00078     (void)u8fnUARTWrite(LABVIEW_SCI, (const uint8_t*)pu8Data, u8Length);
00079     /* End of Frame */
00080     (void)u8fnUARTWrite(LABVIEW_SCI, &cu8EndofFrame, sizeof(cu8EndofFrame));
00081   }
00082   else
00083   {
00084     /* Nothing, we can't transmit, but it's not critical */
00085   }
00086   return;
00087 }
00088 /*
00089  ******************************************************************************
00090  * vfnLVMixAndMatchParallel
00091  ******************************************************************************
00092  */
00093 void vfnLVMixAndMatchParallel(uint8_t* pu8Data, uint8_t u8Length, \
00094                               uint8_t u8Type)
00095 {
00096   uint8_t u8SizeOfTx;
00097   
00098   /* Since this routine uses a shared global variable to construct its    */
00099   /* messages, we must make sure there is no on-going transmission before */
00100   /* constructing it, or else we might corrupt the ongoing transmission.  */
00101   if(CLEAR == gau8LINUARTWordsTx[LABVIEW_SCI])
00102   {
00103     /* We'll place everything within gau8LabViewMessage and start a tx    */
00104     /* afterwards.                                                        */
00105     u8SizeOfTx = SIZE_OF_SOF;
00106     vfnCopyArray((uint8_t*)&(cu8StartOfFrame[SIZE_OF_SOF * u8Type]), \
00107                  gau8LabViewMessage, (uint16_t)u8SizeOfTx);
00108 
00109     /* Transform the length value into an ASCII reading - still in hex */
00110     vfnHexToASCII((uint8_t*)&u8Length, \
00111                   (uint8_t*)&(gau8LabViewMessage[u8SizeOfTx]), \
00112                   sizeof(u8Length));
00113     u8SizeOfTx += (sizeof(u8Length) * 2u);
00114     
00115     /* Once it's done, copy data. */
00116     vfnCopyArray((uint8_t*)pu8Data, (uint8_t*)&(gau8LabViewMessage[u8SizeOfTx]), \
00117                  u8Length);
00118     u8SizeOfTx += u8Length;
00119     /* End of Frame */
00120     vfnCopyArray((uint8_t*)&cu8EndofFrame, \
00121                  (uint8_t*)&(gau8LabViewMessage[u8SizeOfTx]),\
00122                  sizeof(cu8EndofFrame));
00123     u8SizeOfTx += sizeof(cu8EndofFrame);
00124     
00125     /* Start SM for UART Send. If already on, this instruction will be */
00126     /* ignored.                                                        */
00127     (void)u8fnLINUARTSend(LABVIEW_SCI, (const uint8_t*)&gau8LabViewMessage, \
00128                           u8SizeOfTx);
00129   }
00130   else
00131   {
00132     /* No status byte so far. */
00133   }
00134     
00135   
00136   return;
00137 }
00138 /*
00139  ******************************************************************************
00140  * u8fnLabViewSendMS
00141  ******************************************************************************
00142  */
00143 uint8_t  u8fnLabViewSendMSorMP(uint8_t *pu8Data, uint8_t u8Length, \
00144                                uint8_t u8Mode, uint8_t u8LabViewMsgType)         
00145 {
00146   /*
00147   MESSAGE FRAME
00148 
00149   ·     « [«            START
00150   ·      «Type»         2 characters : Identify type of data being sent
00151                                     « MS»       Follow up message displayed in GUI 
00152                                 « MP »  Pop up message displayed as a pop up with user to be required to press OK
00153   ·     « NB »  4 characters: Number of characters sent
00154   ·     C1 characters 1,
00155   ·     C2 characters 2,
00156   ·     C3 characters 3,
00157   ·     C4 characters 4,
00158   ·     C5 characters 5,
00159   ·     C6 characters 6,
00160   ·     …
00161   ·     « ] »           END
00162   
00163   */
00164   (*pvfnMixNMatch[u8Mode])(pu8Data, u8Length, u8LabViewMsgType);
00165 
00166   return(CLEAR);
00167 }
00168 /*
00169  ******************************************************************************
00170  * vfnDisplaySCIData
00171  ******************************************************************************
00172  */
00173 void vfnDisplaySCIData(uint8_t u8Type, uint8_t u8BitSizeOfSample, \
00174                        uint8_t u8NOfStreams, uint16_t u16NOfSamples, \
00175                        uint8_t* pu8Data)
00176 {
00177   /* Declare local variables */
00178   uint8_t  au8LengthInHexASCII[2u];
00179   uint8_t  au8SamplesInHexASCII[4u];
00180   uint8_t  au8DataInHexASCII[2u];
00181   uint32_t u32TotalSize;
00182   uint8_t  u8Status;
00183   //uint8_t  au8FakeTime[4u] = "1234";
00184   
00185   /* Init local variables */
00186   u8Status      = CLEAR;
00187   
00188   /* u16NoOfSamples determines how many samples there is per stream.     */
00189   /* u8NoOfStreams identifies the number of sources available            */
00190   /* u8BitSizeSample is used as an index for either 8, 16, 24, or 32 bit */
00191   /* data per sample per stream                                         */
00192   u32TotalSize  = (uint32_t)(u16NOfSamples * u8NOfStreams * \
00193                                      u8BitSizeOfSample);
00194   
00195   /* Decide between DA or DS in the init of the message */
00196   switch(u8Type)
00197   {
00198     case(LV_DA_MSG):
00199     {
00200       (void)u8fnUARTWrite(LABVIEW_SCI, (const uint8_t*)\
00201                                        (&(cu8StartOfFrameD[CLEAR])),\
00202                           SIZE_OF_SOF_FOR_D);
00203       break;
00204     }
00205     case(LV_DS_MSG):
00206     {
00207       (void)u8fnUARTWrite(LABVIEW_SCI, (const uint8_t*)\
00208                                      (&(cu8StartOfFrameD[SIZE_OF_SOF_FOR_D])),\
00209                           SIZE_OF_SOF_FOR_D);
00210        break;
00211     }
00212     default:
00213     {
00214       /* We have an error */
00215       u8Status = TRUE;
00216       break;
00217     }
00218   };
00219   
00220   /* Only continue if we can */
00221   if(CLEAR == u8Status)
00222   {
00223     /* Send the number of streams */
00224     vfnHexToASCII(&u8NOfStreams, (uint8_t*)&au8LengthInHexASCII, 1u);
00225     (void)u8fnUARTWrite(LABVIEW_SCI, \
00226                         (const uint8_t*)&(au8LengthInHexASCII[1u]), (uint8_t)1u);
00227     
00228     /* Send the number of samples */
00229     vfnHexToASCII((uint8_t*)&u16NOfSamples, au8SamplesInHexASCII, \
00230                   sizeof(u16NOfSamples));
00231     (void)u8fnUARTWrite(LABVIEW_SCI, (const uint8_t*)&au8SamplesInHexASCII, \
00232                         sizeof(au8SamplesInHexASCII));
00233     
00234     /* Send Fake Time */
00235     (void)u8fnUARTWrite(LABVIEW_SCI, (const uint8_t*)&cau8FakeTime, \
00236                         sizeof(cau8FakeTime));
00237     /* Send data. All of it */
00238     for(; u32TotalSize > CLEAR; u32TotalSize--)
00239     {
00240        vfnHexToASCII(pu8Data, au8DataInHexASCII, 1u);
00241        (void)u8fnUARTWrite(LABVIEW_SCI, (const uint8_t*)au8DataInHexASCII, \
00242                         sizeof(au8DataInHexASCII));
00243        pu8Data++;
00244        
00245     }
00246     
00247     /* Send EOF */
00248     (void)u8fnUARTWrite(LABVIEW_SCI, &cu8EndofFrame, sizeof(cu8EndofFrame));
00249     
00250     
00251   }
00252   else
00253   {
00254     /* Exit */
00255   }
00256   
00257   return;
00258 }
00259 /*
00260  ******************************************************************************
00261  * vfnDisplaySCIDataParallel
00262  ******************************************************************************
00263  */
00264 void vfnDisplaySCIDataParallel(uint8_t u8Type, uint8_t u8BitSizeOfSample, \
00265                        uint8_t u8NOfStreams, uint16_t u16NOfSamples, \
00266                        uint8_t* pu8Data)
00267 {
00268   /* Declare local variables */
00269   uint8_t  au8LengthInHexASCII[2u];
00270   uint32_t u32TotalSize;
00271   uint8_t  u8Status;
00272   uint8_t  u8TxSize;
00273   uint8_t  u8SOFIndex;
00274   
00275   /* Init local variables */
00276   u8Status = CLEAR;
00277   u8TxSize = CLEAR;
00278   u8SOFIndex = CLEAR;
00279   
00280   /* Only continue if we don't have an ongoing transmission.  */
00281   if(CLEAR == gau8LINUARTWordsTx[LABVIEW_SCI])
00282   {
00283     /* u16NoOfSamples determines how many samples there is per stream.     */
00284     /* u8NoOfStreams identifies the number of sources available            */
00285     /* u8BitSizeSample is used as an index for either 8, 16, 24, or 32 bit */
00286     /* data per sample per stream                                         */
00287     u32TotalSize  = (uint32_t)(u16NOfSamples * u8NOfStreams * \
00288         u8BitSizeOfSample);
00289     
00290     /* Decide between DA or DS in the init of the message */
00291     if(LV_DA_MSG == u8Type)
00292     {
00293       u8SOFIndex = CLEAR;
00294     }
00295     else if(LV_DS_MSG == u8Type)
00296     {
00297       u8SOFIndex = SIZE_OF_SOF_FOR_D;
00298     }
00299     else
00300     {
00301       u8Status = TRUE;
00302     }
00303         
00304     /* Only continue if we can */
00305     if(CLEAR == u8Status)
00306     {
00307       /* Copy the correct array to the right location */
00308       vfnCopyArray((uint8_t*)&(cu8StartOfFrameD[u8SOFIndex]), \
00309                    (uint8_t*)&gau8LabViewMessage, SIZE_OF_SOF_FOR_D);
00310       u8TxSize += SIZE_OF_SOF_FOR_D;
00311       
00312       /* Send the number of streams */
00313       vfnHexToASCII(&u8NOfStreams, (uint8_t*)&au8LengthInHexASCII, \
00314                     sizeof(u8NOfStreams));
00315       vfnCopyArray((uint8_t*)&(au8LengthInHexASCII[1u]), \
00316                    (uint8_t*)&(gau8LabViewMessage[u8TxSize]), 1u);
00317       u8TxSize++;
00318       
00319       /* Send the number of samples */
00320       vfnHexToASCII((uint8_t*)&u16NOfSamples, \
00321                     (uint8_t*)&(gau8LabViewMessage[u8TxSize]), \
00322                     sizeof(u16NOfSamples));
00323       u8TxSize += (2u * sizeof(u16NOfSamples));
00324 
00325       
00326       /* Send Fake Time */
00327       vfnCopyArray((uint8_t*)&cau8FakeTime, \
00328                    (uint8_t*)&(gau8LabViewMessage[u8TxSize]), sizeof(cau8FakeTime));
00329       u8TxSize += sizeof(cau8FakeTime);
00330 
00331       /* Send data. All of it */
00332       vfnHexToASCII(pu8Data, (uint8_t*)&(gau8LabViewMessage[u8TxSize]), \
00333                     (uint16_t)u32TotalSize);
00334       u8TxSize += ((uint8_t)u32TotalSize * 2u);
00335       
00336       
00337       /* End of Frame */
00338       vfnCopyArray((uint8_t*)&cu8EndofFrame, \
00339                    (uint8_t*)&(gau8LabViewMessage[u8TxSize]),\
00340                    sizeof(cu8EndofFrame));
00341       u8TxSize += sizeof(cu8EndofFrame);
00342           
00343       /* Start SM for UART Send. If already on, this instruction will be */
00344       /* ignored.                                                        */
00345       (void)u8fnLINUARTSend(LABVIEW_SCI, (const uint8_t*)&gau8LabViewMessage, \
00346                             u8TxSize);
00347     }
00348     else
00349     {
00350       /* Exit */
00351     }
00352   }
00353   else
00354   {
00355     /* Not good */
00356     u8Status = 1u;
00357   }
00358   return;
00359 }
00360 /*
00361  ******************************************************************************
00362  * vfnLVTriggerEvent
00363  ******************************************************************************
00364  */
00365 void vfnLVTriggerEvent(void)
00366 {
00367   /* Send Event Frame */
00368   (void)u8fnUARTWrite(LABVIEW_SCI, (uint8_t*)cu8StartOfFrameE, \
00369                            sizeof(cu8StartOfFrameE));
00370   (void)u8fnUARTWrite(LABVIEW_SCI, &cu8EndofFrame, sizeof(cu8EndofFrame));
00371 
00372   return;
00373 }
00374 /*
00375  ******************************************************************************
00376  * vfnDisplaySCIRegistersInTable
00377  ******************************************************************************
00378  */
00379 void vfnDisplaySCIRegistersInTable(uint8_t u8TableType, \
00380                                    uint16_t u16InitAddress, \
00381                                    uint16_t u16NOfData, \
00382                                    uint8_t* pu8RegTable)      
00383 {
00384   /* Declare local variables */
00385   uint8_t u8Status;
00386   uint8_t au8InitAddressASCII[4u];
00387   uint8_t au8NOfDataASCII[4u];
00388   uint8_t au8NextByteASCII[2u];
00389   
00390   /* Init local variables */
00391   u8Status = CLEAR;
00392   
00393   /* Decide which kind of message we're sending */
00394   if(LV_RS_MSG == u8TableType)
00395   {
00396     (void)u8fnLINUARTSend(LABVIEW_SCI, (const uint8_t*)\
00397                                        (&(cu8StartOfFrameT[CLEAR])),\
00398                         SIZE_OF_SOF_FOR_T);
00399   }
00400   else if(LV_RR_MSG == u8TableType)
00401   {
00402     (void)u8fnLINUARTSend(LABVIEW_SCI, (const uint8_t*)\
00403                                      (&(cu8StartOfFrameT[SIZE_OF_SOF_FOR_T])),\
00404                         SIZE_OF_SOF_FOR_T);
00405   }
00406   else if(LV_RE_MSG == u8TableType)
00407   {
00408     (void)u8fnLINUARTSend(LABVIEW_SCI, (const uint8_t*)\
00409                                 (&(cu8StartOfFrameT[SIZE_OF_SOF_FOR_T * 2u])),\
00410                         SIZE_OF_SOF_FOR_T);
00411   }
00412   else
00413   {
00414     u8Status = TRUE;
00415   }
00416   
00417   /* Continue if we can */
00418   if(CLEAR == u8Status)
00419   {
00420     /* Format asks for a 4-character Address - perform this while the start  */
00421     /* of frame is being sent                                                */
00422     vfnHexToASCII((uint8_t*)&u16InitAddress, (uint8_t*)au8InitAddressASCII, \
00423                   sizeof(u16InitAddress));
00424     
00425     /* Since we're optimizing time, we will also need to send u16NOfData in  */
00426     /* ASCII. Transform it to ASCII now                                      */
00427     vfnHexToASCII((uint8_t*)&u16NOfData, (uint8_t*)au8NOfDataASCII, \
00428                   sizeof(u16NOfData));
00429     
00430     /* Start transmitting when ready */
00431     u8Status = u8fnWaitForUARTTxBufferToEmpty(LABVIEW_SCI, SCI_TIME_OUT);
00432     
00433     if(CLEAR == u8Status)
00434     {
00435       /* Send Address */
00436       (void)u8fnUARTWrite(LABVIEW_SCI, (uint8_t*)au8InitAddressASCII, \
00437                           sizeof(au8InitAddressASCII));
00438       /* Send Number of data to send */
00439       (void)u8fnUARTWrite(LABVIEW_SCI, (uint8_t*)au8NOfDataASCII, \
00440                           sizeof(au8NOfDataASCII));
00441       for(;u16NOfData > CLEAR; u16NOfData--)
00442       {
00443         /* Convert next data to Hex while we have an ongoing transmission */
00444         vfnHexToASCII(pu8RegTable, (uint8_t*)au8NextByteASCII, 1u);
00445         
00446         /* Increment pointer */
00447         pu8RegTable++;
00448         
00449         u8Status = u8fnUARTWrite(LABVIEW_SCI, (uint8_t*)au8NextByteASCII, \
00450                                  sizeof(au8NextByteASCII));
00451         
00452       } /* End FOR */
00453       
00454       /* Send EOF */
00455       u8Status = u8fnWaitForUARTTxBufferToEmpty(LABVIEW_SCI, SCI_TIME_OUT);
00456       if(CLEAR == u8Status)
00457       {
00458         (void)u8fnUARTWrite(LABVIEW_SCI, &cu8EndofFrame, sizeof(cu8EndofFrame));
00459       }
00460       else
00461       {
00462         /* Exit */
00463       } /* end else */
00464     } /* End if */
00465     else
00466     {
00467       /* Exit */
00468     }
00469   } /* End if */
00470   else
00471   {
00472     /* Exit */
00473   }
00474   
00475   return;
00476 }
00477 /*
00478  ******************************************************************************
00479  * u8fnLVConfigRxCmd
00480  ******************************************************************************
00481  */
00482 uint8_t u8fnLVConfigRxCmd(void)
00483 {
00484   return(u8fnLINUARTReceive(LABVIEW_SCI, gau8LabViewRxCmd, CLEAR));
00485 }
00486 /*
00487  ******************************************************************************
00488  * u8fnLVReceiveCmd
00489  ******************************************************************************
00490  */
00491 uint8_t u8fnLVReceiveCmd(void)
00492 {
00493   uint8_t u8Status;
00494   
00495   /* Get all the data */
00496   u8Status = u8fnLINUARTGetDataNoIsr(LABVIEW_SCI);
00497   /* Reset our pointer, etc. */
00498   if(UART_NO_DATA_RX == u8Status)
00499   {
00500     /* Nothing */
00501   }
00502   else
00503   {
00504     u8Status = u8fnLVConfigRxCmd();
00505   }
00506   return(u8Status);
00507 }
00508 /*
00509  ******************************************************************************
00510  * vfnLVEnable
00511  ******************************************************************************
00512  */
00513 void vfnLVEnable(uint8_t u8Enable)
00514 {
00515   vfnEnableLINTxISR(LABVIEW_SCI, u8Enable);
00516  
00517   return;
00518 }
00519 /*
00520  ******************************************************************************
00521  * u8fnLVWaitForTxPort
00522  ******************************************************************************
00523  */
00524 uint8_t u8fnLVWaitForTxPort(void)
00525 {
00526   return(u8fnWaitForUARTTxBufferToEmpty(LABVIEW_SCI, SCI_TIME_OUT));
00527 }
00528 
00529 /*
00530  ******************************************************************************
00531  *
00532  *  End of file.
00533  *
00534  ******************************************************************************
00535  */